var path = require('path');
var ccap = require('ccap')({
    height: 40,
    width: 130,
    fontsize: 25,
    offset: 20,
    quality:100
});

var user = require('./../controllers/user.js');
var post = require('./../controllers/post.js');
var comment = require('./../controllers/comment.js');
var ancmt = require('./../controllers/ancmt.js');

module.exports = function(app) {
    //获取验证码图片并将验证码保存在session中
    app.get('/securityCodeImage', function (req, res) {
        var arr = ccap.get();
        req.session.sc = arr[0];
        res.end(arr[1]);
    });

    //搜索 将搜索用户和post的结果返回
    app.post('/search', function (req, res) {
        if(!req.body.keyword){
            res.json({code: 0, msg: "缺少必要数据~"});
            return false;
        } else {
            var key = req.body.keyword;

            var result = {
                users: null,
                posts: null
            };

            user.searchUser(key, function (users) {
                result.users = users;

                post.searchPost(key, function (posts) {
                    result.posts = posts;

                    res.json({code: 1, result: result});
                });
            });
        }
    });

    /**
     * user routes
     */
	//注册
	app.post('/regist',user.regist);
	//登录
	app.post('/login', user.login);
	//登出
	app.post('/logout', user.logout);
    //根据用户名获取用户信息
    app.post('/getUserInfo', user.getUserInfo);
    //更新头像
    app.post('/updateAvatar', user.updateAvatar);
    //更改邮箱
    app.post('/updateEmail', user.updateEmail);
    //更改密码
    app.post('/updatePwd', user.updatePwd);
    //分页查询用户信息
    app.post('/getUserByPage', user.getUserByPage);
    //删除用户
    app.post('/deleteUser', function(req, res){
        if(req.body.id){
            user.findUserById(req.body.id, function(u){
                if(u){
                    post.deletePostByUserName(u[0].name, function (err) {
                        if(err){
                            console.log(err);
                            res.json({code: 0, msg: '数据库删除错误！'});
                        } else {
                            comment.deleteCommentsByUserName(u[0].name, function(err){
                                if(err){
                                    console.log(err);
                                    res.json({code: 0, msg: '数据库删除错误！'});
                                } else{
                                    user.deleteUserById(u[0]._id, function (err) {
                                        if(err){
                                            console.log(err);
                                            res.json({code: 0, msg: '数据库删除错误！'});
                                        }else {
                                            res.json({code: 1, msg: '删除完成！'});
                                        }
                                    });
                                }
                            });
                        }
                    });
                } else {
                    res.json({code: 0, msg: '数据库查询错误！'});
                }
            });
        } else {
            res.json({code: 0, msg: '缺少必要数据！'});
        }
    });

	//个人主页
	app.get('/home', function(req, res){

		if(!req.session.sid){

			res.json({code: 0, msg: '您已经退出~'});

		}
	});

    /**
     * post routes
     */
    app.post('/post', post.getPostById);
    //获取发布列表
    app.post('/posts', post.getPosts);
    //发布
    app.post('/say', post.say);
    //删除
    app.post('/deletePost', post.deletePost);
	//赞
	app.post('/vote', post.vote);
    //编辑
    app.post('/editPost', post.editPost);

    /**
     * comment routes
     */
	//获取评论
	app.post('/comments', comment.getComments);
	//评论 
	app.post('/comment',comment.addComment);
	//删除评论
	app.post('/deleteComment', comment.deleteComment);

    /**
     * admin routes
     */
    //管理员登陆
    app.get('/admin/login', function(req, res){
        console.log("00000");
        res.sendFile(path.join(__dirname, '../', '/public/adminLogin.html'));
    });

    app.post('/admin/login', function(req, res){
        if(req.body.name != 'admin' || req.body.pwd != '123'){
            res.json({code: 0, msg: '用户名或密码错误！'});
        } else {
            res.sendFile(path.join(__dirname, '../', '/public/adminIndex.html'));
        }
    });

    /**
     * 公告routes
     */
    app.get('/ancmts', ancmt.findAll);

    app.get('/ancmt', ancmt.findOne);

    app.post('/addAncmt', ancmt.addAncmt);

    app.post('/deleteAncmt', ancmt.deleteOne);


	app.get('*', function (req, res){
		res.sendFile(path.join(__dirname, '../', '/public/index.html'));
	});
    
};